<?php
namespace app\admin\controller;
use think\Db;
use think\facade\Request;
class Channel extends Common
{
    public function initialize(){
        parent::initialize(); 
        $this->assign('shoplist',cache('shoplist')); 
        $this->assign('cionlist',cache('cionlist'));  
    }
    //通道列表
    public function index(){
        if(Request::isAjax()) {
            $key = input('post.key'); 
            $where=[];
            $page =input('page')?input('page'):1;
            $userid = input('post.userid');
            if($userid) $where['c.userid']=$userid;  
            $shopid = input('post.shopid');
            if($shopid) $where['c.shopid']=$shopid;   
            $agentid = input('post.agentid');
            if($agentid) $where['c.agentid']=$agentid;  
            $pageSize =input('limit')?input('limit'):config('pageSize');
             $list=db('channel')->alias('c')
                ->join(config('database.prefix').'users u','u.id = c.userid','left') 
                ->join(config('database.prefix').'shop s','s.id = c.shopid','left') 
                ->field('c.*,u.username,u.mobile,u.agentlv,s.lv as shoprate')
                ->where('c.title|u.username|u.mobile','like',"%".$key."%")->where($where)
                ->order('c.id desc')
                ->paginate(array('list_rows'=>$pageSize,'page'=>$page))
                ->toArray(); 
            foreach ($list['data'] as $k=>$v){
                $list['data'][$k]['addtime'] = date('Y-m-d H:i',$v['addtime']);  
                $list['data'][$k]['shopname'] = $v['shopname'].'('.$v['shoprate'].'%)'; 
                if($v['agentlv']>0) $list['data'][$k]['username'] = $v['username'].'('.$v['agentlv'].'%)';  
            }
            return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
        }
        return $this->fetch();
    }
    public function channelmin(){
        $this->assign('userid',input('userid')); 
        $this->assign('shopid',input('shopid')); 
        $this->assign('agentid',input('agentid')); 
        return $this->fetch();
    } 
    //获取通道
    public function getChannel(){
        $list=Db::name("channel")->where('shopid',input("shopid"))->select();
        return $list;
    }
    public function edit(){
        if(Request::isAjax()) {
            $data = Request::except('file'); 
            $shopid =explode(':',$data['shopid']);
            $data['shopid'] = $shopid[1]; 
            $shopappid=db('shop')->where('id',$data['shopid'])->find();
            $data['shopappid']=$shopappid['appid'];
            $userss=db('users')->where('id',$data['userid'])->find();  
            $shoplv=explode('|',$shopappid['lv']);
            $agentlv=explode('|',$userss['agentlv']);
            $channellv=explode('|',$data['rate']);             
            if($channellv[0]>=($shoplv[0]-$agentlv[0]) ) return $result = ['code'=>-1,'msg'=>'【微信】手续费过高'];
            if($channellv[1]>=($shoplv[1]-$agentlv[1]) ) return $result = ['code'=>-1,'msg'=>'【支付宝】手续费过高'];
            if($channellv[2]>=($shoplv[2]-$agentlv[2]) ) return $result = ['code'=>-1,'msg'=>'【银行卡】手续费过高'];
            $data['agentid'] = $userss['agentid']; 
            $cionid =explode(':',$data['cionid']);
            $data['cionid'] = $cionid[1]; 
            db('channel')->update($data);
            $result['code'] = 1;
            $result['msg'] = '修改成功!';
            $result['url'] = url('index');
            return $result;
        }else{
            $id=input('id');
            $Info=db('channel')->where(array('id'=>$id))->find();
            $this->assign('info',json_encode($Info,true));
            $this->assign('title',lang('edit').'通道');
 
            $shoplist = cache('shoplist');
            $this->assign('shoplist',json_encode($shoplist,true)); 
            $cionlist = cache('cionlist');
            foreach ($cionlist as $k=>$v){
                $cionlist[$k]['name'] = $v['name'].'/'.$v['name_en'];
            }
            $this->assign('cionlist',json_encode($cionlist,true)); 

            return $this->fetch();
        }
    }
    public function addchannel(){
        if(request()->isPost()) {
            $data = input('post.');  
            $data['addtime'] = time(); 
            $cionprice =db('shop_cion')->where('id',$data['cionid'])->value('price');
            $data['price']=$cionprice; 
            $shopappid=db('shop')->where('id',$data['shopid'])->find();
            $data['shopappid']=$shopappid['appid']; 
            $userss=db('users')->where('id',$data['userid'])->find(); 
            if($userss['is_lock']==1) return array('code'=>0,'msg'=>'该码商尚未来激活'); 
            $userbank=db('user_banks')->where('userid',$data['userid'])->where('open',1)->find();            
            if($userbank==false) return array('code'=>0,'msg'=>'该码商尚未设置收款账户');  
            $data['agentid'] = $userss['agentid']; 
            $data['appid'] = $userss['appid']; 
            $data['appkey'] = $userss['appkey'];  
            $data['open'] = 1;

            $shoplv=explode('|',$shopappid['lv']);
            $agentlv=explode('|',$userss['agentlv']);
            $channellv=explode('|',$data['rate']);             
            if($channellv[0]>=($shoplv[0]-$agentlv[0]) ) return $result = ['code'=>-1,'msg'=>'【微信】手续费过高'];
            if($channellv[1]>=($shoplv[1]-$agentlv[1]) ) return $result = ['code'=>-1,'msg'=>'【支付宝】手续费过高'];
            if($channellv[2]>=($shoplv[2]-$agentlv[2]) ) return $result = ['code'=>-1,'msg'=>'【银行卡】手续费过高'];

            $id = db('channel')->insertGetId($data);
            if($id){ 
                return array('code'=>1,'msg'=>'开通成功！');
            }else{
                return array('code'=>-1,'msg'=>'开通失败'); 
            }
        }else{
            $this->assign('title','新增通道');
            $this->assign('thisuserid',input('userid'));
            $this->assign('userlv',input('userlv'));
            return $this->fetch();
        }
    }
    //设置状态
    public function editState(){  
        if(db('channel')->where(array('id'=>input('id')))->update(array('open'=>input('open')))!==false){
            return ['status'=>1,'msg'=>'设置成功!'];
        }else{
            return ['status'=>0,'msg'=>'设置失败!'];
        }
    } 
    public function del(){
        $channel=db('channel')->where('id',input('id'))->find();
        if($channel['money_total']>0){
            return ['code'=>-1,'msg'=>'改通道已有订单不能删除，您可将其关闭！'];
        }else{
            db('channel')->where(array('id'=>input('id')))->delete();
            return ['code'=>1,'msg'=>'删除成功！'];
        }
    }  
}